package com.example.gss.controller;

import com.example.gss.entity.SocialService;
import com.example.gss.Service.SocialServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/socialservices")
public class SocialServiceController {

    @Autowired
    private SocialServiceService socialServiceService;

    /**
     * 创建社会服务记录
     */
    @PostMapping
    public ResponseEntity<SocialService> createService(@RequestBody SocialService service) {
        SocialService createdService = socialServiceService.createService(service);
        return new ResponseEntity<>(createdService, HttpStatus.CREATED);
    }

    /**
     * 获取所有社会服务记录
     */
    @GetMapping
    public ResponseEntity<List<SocialService>> getAllServices() {
        List<SocialService> services = socialServiceService.getAllServices();
        return new ResponseEntity<>(services, HttpStatus.OK);
    }

    /**
     * 根据ID获取社会服务记录
     */
    @GetMapping("/{serviceId}")
    public ResponseEntity<SocialService> getServiceById(@PathVariable String serviceId) {
        Optional<SocialService> serviceOptional = socialServiceService.getServiceById(serviceId);
        return serviceOptional.map(service -> new ResponseEntity<>(service, HttpStatus.OK))
                .orElseGet(() -> new ResponseEntity<>(HttpStatus.NOT_FOUND));
    }

    /**
     * 更新社会服务记录
     */
    @PutMapping("/{serviceId}")
    public ResponseEntity<SocialService> updateService(@PathVariable String serviceId, @RequestBody SocialService service) {
        service.setServiceId(serviceId);
        SocialService updatedService = socialServiceService.updateService(service);
        return new ResponseEntity<>(updatedService, HttpStatus.OK);
    }

    /**
     * 删除社会服务记录
     */
    @DeleteMapping("/{serviceId}")
    public ResponseEntity<Void> deleteService(@PathVariable String serviceId) {
        socialServiceService.deleteService(serviceId);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 根据学生ID查询社会服务记录
     */
    @GetMapping("/search/student")
    public ResponseEntity<List<SocialService>> findServicesByStudentId(@RequestParam String studentId) {
        List<SocialService> services = socialServiceService.findServicesByStudentId(studentId);
        return new ResponseEntity<>(services, HttpStatus.OK);
    }

    /**
     * 根据类型查询社会服务记录
     */
    @GetMapping("/search/type")
    public ResponseEntity<List<SocialService>> findServicesByType(@RequestParam String type) {
        List<SocialService> services = socialServiceService.findServicesByType(type);
        return new ResponseEntity<>(services, HttpStatus.OK);
    }
}